package com.weigj.model;

import java.util.ArrayList;
import java.util.List;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.Record;

public class CarInfo extends Model<CarInfo>{
	public static final CarInfo dao=new CarInfo();
	
	/**
	 * 根据车牌号或车架号查询车辆信息
	 * @param carParam  车架或车牌号 车架号一般为17位 
	 * @return
	 */
	public CarInfo queryCarInfo(String carParam){
		String sql ="";
		if(carParam.length()>10){
			sql="select * from app_carInfo c where c.enable=1 and c.infoframcode=?";
		}else{
			sql="select * from app_carInfo c where c.enable=1 and c.infolicense=?";
		}
		return dao.findFirst(sql, carParam);
	}
	/**
	 * 查询场站内的车型待租车辆的总数
	 * @param localId 场站Id
	 * @param carTypeId 车型Id
	 * @param limit  限行规则
	 * @return 
	 */
	public Integer queryCarByCarType(Long localId,Long carTypeId,String limit){
		System.out.println(localId +"+"+carTypeId+"+"+limit+"===============");
		String sql="";
		if(null!=limit&&!limit.equals("")){
			sql="select count(*) from app_carInfo c where c.localId=? and c.topcarmodelid=? and c.carState=? and substr(translate(c.infolicense,'0123456789'||c.infolicense,'0123456789'),length(translate(c.infolicense,'0123456789'|| c.infolicense,'0123456789'))) not in(?)";
			return Db.queryBigDecimal(sql, localId,carTypeId,"待租",limit).intValue();
		}else{
			sql="select count(*) from app_carInfo c where c.localId=? and c.topcarmodelid=? and c.carState=?";
			return Db.queryBigDecimal(sql, localId,carTypeId,"待租").intValue();
		}
	}
	public  CarInfo queryCarBytype(Long localId,Long carTypeId,String limit){
		String sql="";
		if(null!=limit&&!limit.equals("")){
			sql="select c.* from app_carInfo c where c.localId=? and c.topcarmodelid=? and c.carState=? and substr(translate(c.infolicense,'0123456789'||c.infolicense,'0123456789'),length(translate(c.infolicense,'0123456789'|| c.infolicense,'0123456789'))) not in(?)";
			return dao.findFirst(sql, localId,carTypeId,"待租",limit);
		}else{
			sql="select c.* from app_carInfo c where c.localId=? and c.topcarmodelid=? and c.carState=?";
			return dao.findFirst(sql, localId,carTypeId,"待租");
		}
		
	}
	
	public CarInfo queryCarBytype(Long localId,Long carTypeId){
		String sql="select c.* from app_carInfo c where c.localId=? and c.topcarmodelid=? and c.carState=?";
		return dao.findFirst(sql, localId,carTypeId,"待租");
	}
	public String queryCarKM(String infoId){
		Record r = new Record();
		String km ="0";
		String sql = "select t.DYNAMETER from APP_INFODYNAMIC t where t.INFOID=?";
		r=Db.findFirst(sql,infoId);
		km=r.getBigDecimal("DYNAMETER").toString();
		return km;
	}
	public String getCarLocalName(String localId){
		String localName = "";
		String sql = "select t.deptname from app_cardept t where t.deptid=?";
		localName=Db.findFirst(sql,localId).getStr("DEPTNAME");
		return localName;
	}
	public String getCarType(String infoId){
		String carType ="";
		String sql = "select t.tcm_wholename from car_detail_info t where t.infoid=?";
		carType=Db.findFirst(sql,infoId).getStr("TCM_WHOLENAME");
		return carType;
	}
	
	public List<CarInfo> queryCarVague(String carNum,String localId)
	{
		List<CarInfo> carArray = new ArrayList<CarInfo>();
		String sql="select c.* from app_carInfo c where c.localId=? and c.infolicense like \'%"+carNum+"%\' and c.carState=? and c.enable=1";
		carArray=dao.find(sql,localId,"待租");
		return carArray;
	}
	public CarInfo queryCanBackCar(String carNum)
	{
		CarInfo car = new CarInfo();
		String sql="select c.* from app_carInfo c where c.infolicense = ? and c.carState=? and c.enable=1";
		car=dao.findFirst(sql,carNum,"出租");
		return car;
	}
}
